package com.opensource.lzx.handler;

import com.opensource.lzx.cache.ServerManager;
import com.opensource.lzx.ssh.RestartSupport;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ServerIdleHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Client register: " + ctx.channel().id().asLongText());
        ServerManager.register(ctx.channel().id().asLongText(), ctx.channel());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Client : " + ctx.channel().id().asLongText() + " disconnect.");
        String channelId = ctx.channel().id().asLongText();
        ServerManager.removeServer(channelId);
        RestartSupport.restart(ServerManager.getAndRemoveHostInfo(channelId));
    }
}







